package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.DelReq;
import com.cloud.lan.api.dto.req.LanUserWhiteListReq;
import com.cloud.lan.dao.LanManageUserMapper;
import com.cloud.lan.dao.LanUserWhiteListMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.base.exceptions.BaseException;
import com.cloud.lan.entity.enums.LanError;
import com.cloud.lan.entity.model.dataobject.LanManageUserDO;
import com.cloud.lan.entity.model.dataobject.LanUserWhiteListDO;
import com.cloud.lan.entity.utils.VocUtil;
import com.cloud.lan.service.ILanUserWhiteListService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ONE;
import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ZERO;

/**
 * <p>
 * 用户白名单表 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2022-02-10
 */
@Slf4j
@Service
public class LanUserWhiteListServiceImpl extends ServiceImpl<LanUserWhiteListMapper, LanUserWhiteListDO> implements ILanUserWhiteListService {

    @Resource
    LanUserWhiteListMapper lanUserWhiteListMapper;
    @Resource
    LanManageUserMapper lanManageUserMapper;

    @Override
    public BaseResponse manageList(LanUserWhiteListReq req) {
        log.info("#查询 用户白名单 列表 {}", req);
        LambdaQueryWrapper<LanUserWhiteListDO> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(LanUserWhiteListDO::getCreateTime);
        Page<LanUserWhiteListDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        IPage<LanUserWhiteListDO> pageList = lanUserWhiteListMapper.selectPage(page, qw);
        return BaseResponse.success(pageList);
    }

    @Override
    public BaseResponse add(LanUserWhiteListReq req) {
        log.info("#新增 用户白名单表 {}", req);
        LanUserWhiteListDO lanUserWhiteListDO = new LanUserWhiteListDO();
        BeanUtil.copyProperties(req, lanUserWhiteListDO);
        LambdaQueryWrapper<LanUserWhiteListDO> luwQw = new LambdaQueryWrapper<>();
        luwQw.eq(LanUserWhiteListDO::getOpenId, lanUserWhiteListDO.getOpenId());
        if (lanUserWhiteListMapper.selectCount(luwQw) > ZERO.getKey()) {
            log.error("{}", LanError.LAN_030002.getMsg());
            throw new BaseException(LanError.LAN_030002);
        }
        LanManageUserDO lanManageUserDO = lanManageUserMapper.
                selectOne(new LambdaQueryWrapper<LanManageUserDO>().eq(LanManageUserDO::getToken, req.getToken()));
        lanUserWhiteListDO.setOperator(lanManageUserDO.getLoginName());
        lanUserWhiteListMapper.insert(lanUserWhiteListDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse update(LanUserWhiteListReq req) {
        log.info("#更新 用户白名单表 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId())}, new String[]{"id"}, LanError.LAN_030001.getCode());
        LanUserWhiteListDO lanUserWhiteListDO = lanUserWhiteListMapper.selectById(req.getId());
        BeanUtil.copyProperties(req, lanUserWhiteListDO);
        LanManageUserDO lanManageUserDO = lanManageUserMapper.
                selectOne(new LambdaQueryWrapper<LanManageUserDO>().eq(LanManageUserDO::getToken, req.getToken()));
        lanUserWhiteListDO.setOperator(lanManageUserDO.getLoginName());
        lanUserWhiteListDO.setUpdateTime(DateUtil.date());
        lanUserWhiteListMapper.updateById(lanUserWhiteListDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse delete(DelReq req) {
        log.info("#删除 用户白名单表 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId())}, new String[]{"id"}, LanError.LAN_030001.getCode());
        LanManageUserDO lanManageUserDO = lanManageUserMapper.
                selectOne(new LambdaQueryWrapper<LanManageUserDO>().eq(LanManageUserDO::getToken, req.getToken()));
        LanUserWhiteListDO lanUserWhiteListDO = lanUserWhiteListMapper.selectById(req.getId());
        lanUserWhiteListDO.setOperator(lanManageUserDO.getLoginName());
        lanUserWhiteListDO.setIsDeleted(ONE.getKey());
        lanUserWhiteListDO.setUpdateTime(DateUtil.date());
        lanUserWhiteListMapper.deleteById(lanUserWhiteListDO);
        return BaseResponse.success();
    }
}
